package com.xbfinance.portal.service.impl;

import com.xbfinance.common.util.JedisClient;
import com.xbfinance.common.util.JedisClientCluster;
import com.xbfinance.common.util.JsonUtils;
import com.xbfinance.common.util.StrKit;
import com.xbfinance.portal.dao.BannerPicMapper;
import com.xbfinance.portal.pojo.po.BannerPic;
import com.xbfinance.portal.pojo.po.BannerPicExample;
import com.xbfinance.portal.service.BannerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BannerServiceImpl implements BannerService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BannerPicMapper bannerPicMapper;

    @Autowired
    private JedisClient jedisClient;

    @Override
    public List<BannerPic> queryBanner(Integer cid) {

        try {

            String json = jedisClient.hget("CONTENTLIST", cid + "");

            if(StrKit.notBlank(json)){
                List<BannerPic> bannerPics = JsonUtils.jsonToList(json, BannerPic.class);
                return bannerPics;
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }

        BannerPicExample example = new BannerPicExample();
        example.createCriteria().andCidEqualTo(cid);
        List<BannerPic> bannerPics = bannerPicMapper.selectByExample(example);

        try {

            jedisClient.hset("CONTENTLIST",cid+"",JsonUtils.objectToJson(bannerPics));

        }catch (Exception e){
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }

        return bannerPics;
    }
}
